package com.moo2u2.android.oaktononthego.crm;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.moo2u2.android.oaktononthego.R;
import com.moo2u2.android.oaktononthego.crm.dao.CustomerDbAdapter;
import com.moo2u2.android.oaktononthego.crm.dao.CustomerTable;

public class CRMActivity extends ListActivity {

	private CustomerDbAdapter dbHelper;

	private Cursor cursor;

	private static final int ACTIVITY_CREATE = 0;

	private static final int ACTIVITY_EDIT = 1;

	private static final int EDIT_ID = Menu.FIRST + 1;

	private static final int DELETE_ID = Menu.FIRST + 2;

	/** {@inheritDoc} */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.customer_list);
		if (customTitleSupported)
			getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
					R.layout.window_title);

		dbHelper = new CustomerDbAdapter(this);
		dbHelper.open();
		if (!dbHelper.hasData()) {
			fakeData(dbHelper);
		}
		fillData();
		registerForContextMenu(getListView());
	}

	private void fillData() {
		cursor = dbHelper.fetchAllCustomers();
		startManagingCursor(cursor);

		String[] from = new String[] { CustomerTable.KEY_NAME,
				CustomerTable.KEY_ADDRESS };
		int[] to = new int[] { R.id.customer, R.id.address };

		SimpleCursorAdapter notes = new ImageCursorAdapter(this,
				R.layout.customer_row, cursor, from, to);
		setListAdapter(notes);
	}

	private void createCustomer() {
		Intent i = new Intent(this, CRMDetail.class);
		i.putExtra(CRMDetail.MODE, CRMDetail.Mode.EDIT);
		startActivityForResult(i, ACTIVITY_CREATE);
	}

	private void editCustomer(long id) {
		Intent i = new Intent(this, CRMDetail.class);
		i.putExtra(CustomerTable.KEY_ROWID, id);
		i.putExtra(CRMDetail.MODE, CRMDetail.Mode.EDIT);
		startActivityForResult(i, ACTIVITY_EDIT);
	}

	private void viewCustomer(long id) {
		Intent i = new Intent(this, CRMDetail.class);
		i.putExtra(CustomerTable.KEY_ROWID, id);
		i.putExtra(CRMDetail.MODE, CRMDetail.Mode.READ);
		startActivityForResult(i, ACTIVITY_EDIT);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		viewCustomer(id);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.crm, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.crm_add:
			createCustomer();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, EDIT_ID, 0, R.string.edit);
		menu.add(0, DELETE_ID, 0, R.string.delete);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		case EDIT_ID:
			editCustomer(info.id);
			return true;
		case DELETE_ID:
			dbHelper.deleteCustomer(info.id);
			fillData();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		fillData();

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (dbHelper != null) {
			dbHelper.close();
		}
	}

	private void fakeData(CustomerDbAdapter dbHelper) {
		dbHelper.createCustomer(
				"GE Money",
				"Jason Woods",
				"1234 5678",
				"1234 5678",
				"jason.woods@ge.com",
				"243 George St, Sydney",
				1,
				0,
				"GE Capital is one of our bigger clients.  While their head office is in Melbourne, there's still lots of space up in Sydney!",
				"Add your notes here");
		dbHelper.createCustomer(
				"ING",
				"James Donohoe",
				"1234 5678",
				"1234 5678",
				"james.donohoe@ing.com",
				"140 Sussex St, Sydney",
				1,
				0,
				"Two current projects at ING Direct. One is providing implementation services for their Superannunation project, and the other is for their Adviser project  ",
				"Add your notes here");
	}
}
